package no.ntnu.fp.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.border.EtchedBorder;

import net.miginfocom.swing.MigLayout;
import no.ntnu.fp.model.Appointment;
import no.ntnu.fp.model.Message;
import no.ntnu.fp.model.Person;
import no.ntnu.fp.model.Appointment.RSVP;
import no.ntnu.fp.model.Message.MessageType;

public class MessageLabel extends JLabel {
	
	private Message message;
	private MessageContainer messageContainer;
	private Appointment a;
	public MessageLabel(Message message, MessageContainer m) {

		this.message = message;
		messageContainer = m;

		setForeground(Color.YELLOW);
		setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		setLayout(new MigLayout("wrap 1"));
		/*
		 * En meldingslabel skal ha avsender og tittel p� m�tet.
		 */
		a = message.getAppointment();

		JLabel title = new JLabel(a.getOwner().name);
		
		title.setFont(new Font("Dialog", Font.BOLD, 12));
		add(title, "align center");
		
		JButton jbtn = new JButton("Description");
		jbtn.setBackground(Color.MAGENTA);
		jbtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				viewDetails();
			}
		});
		add(jbtn, "align center");
		setPreferredSize(new Dimension(110,65));
		if(!message.active){
			setVisible(false);
		}
		
	}

	public void viewDetails() {
		
		switch(message.getMessageType()) {
		
			case DEINVITE: {
				
				String info = "\tDU ER IKKE LENGER VELKOMMEN PÅ\nFØLGENDE MØTE\n\n";
				info += "\t" + a.getTitle() + "\n";
				info += a.getDescription();
				
				JOptionPane.showMessageDialog(this, info);
				message.setMessageActiveState(false);
				this.setVisible(false);
				messageContainer.remove(this);
				break;
			}
		
			case MEETING_CHANGED: {
				
				String info =  "\tMØTE ER ENDRET TIL FØLGENDE\n\n";
				info += "\t" + a.getTitle() + "\n";
				info += "Dato: " + a.getStartDate() + "\n";
				info += "Tidspunkt: " + a.getStart() + " - " + a.getEnd() + "\n";
				info += a.getDescription() + "\n";
				
				Object[] options = {"Avbryt", "Avsl\u00E5", "Godta"};	
				int i = JOptionPane.showOptionDialog(this, info, "Tittel",
						JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, 
						null, options, options[0]);
				
				switch(i) {
					case(2): {
						submitAnswer(RSVP.ATTENDING);
						message.setMessageActiveState(false);
						this.setVisible(false);
						messageContainer.remove(this);
						break;
					}
					case(1): {
						submitAnswer(RSVP.NOT_ATTENDING);
						message.setMessageActiveState(false);
						this.setVisible(false);
						messageContainer.remove(this);
						break;
					}
				}
				break;
			}
		
			case MEETING_CANCELED: {
				
				String info = "\tFØLGENDE MØTE ER AVLYST\n\n";
				info += "\t" + a.getTitle() + "\n";
				info += "Dato: " + a.getStartDate() + "\n";
				info += "Tidspunkt: " + a.getStart() + " - " + a.getEnd() + "\n";
				info += a.getDescription() + "\n";
				
				JOptionPane.showMessageDialog(this, info);
				message.setMessageActiveState(false);
				this.setVisible(false);
				messageContainer.remove(this);
				break;
			}
		
			case INVITE: {
				
				String info = "\t" + a.getTitle() + "\n";
				info += "Dato: " + a.getStartDate() + "\n";
				info += "Tidspunkt: " + a.getStart() + " - " + a.getEnd() + "\n";
				info += a.getDescription() + "\n";
				
				Object[] options = {"Avbryt", "Avsl\u00E5", "Godta"};	
				int i = JOptionPane.showOptionDialog(this, info, "Tittel",
						JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, 
						null, options, options[0]);
				
				switch(i) {
					case(2): {
						submitAnswer(RSVP.ATTENDING);
						message.setMessageActiveState(false);
						this.setVisible(false);
						messageContainer.remove(this);
						break;
					}
					case(1): {
						submitAnswer(RSVP.NOT_ATTENDING);
						message.setMessageActiveState(false);
						this.setVisible(false);
						messageContainer.remove(this);
						break;
					}
				}
				
				break;
			}
		}
	}
	public void submitAnswer(RSVP answer){

		a.setRSVP(answer, Person.getCurrentUser());
	}
	
}
